#pragma once

#include "container.h"
#include <cstdlib>
#include <iostream>

container::container() : capacity(1)
{
	digit = 0;
}

container::container(int capacity_) : capacity(capacity_)
{
	if (capacity > 1)
	{
		digits = new int64[capacity];
		for (int i = 0; i < capacity; i++)
			digits[i] = 0;
	} else 
		digit = 0;
}

container::container(const container& other) : capacity(other.capacity)
{
	if (capacity > 1)
	{
		digits = new int64[capacity];
		for (int i = 0; i < capacity; i++)
			digits[i] = other.digits[i];
	} else
		digit = other.digit;
}

container::container(const container& other, int capacity_) : capacity(capacity_)
{
	if (capacity > 1) 
	{
		digits = new int64[capacity];
		for (int i = 0; i < capacity; i++)
			digits[i] = other[i];
	}
	else 
		digit = other[0];
}

container::~container()
{
	if (capacity > 1)
		delete[] digits;
}

int64 container::operator [](int index) const
{
	if (index < 0 || index >= capacity) {
		return  0;
	} else
		return (capacity == 1) ? digit : digits[index];
}

int64& container::operator [](int index)
{
	if (index < 0 || index >= capacity) 
		throw Range();
	else
		return (capacity == 1) ? digit : digits[index];
}

container& container::operator =(const container& other)
{
	if (this == &other)
		return (*this);
	if (capacity > 1)
		delete[] digits;
	capacity = other.capacity;
	if (capacity > 1)
	{
		digits = new int64[capacity];
		for (int i = 0; i < capacity; i++)
			digits[i] = other.digits[i];
	} else
	{
		digit = other.digit;
	}
	return *this;
}

int container::size() const
{
	return capacity;
}

void container::swap(container& other)
{
	std::swap(capacity, other.capacity);
	if (sizeof(int64) > sizeof(int64*))
		std::swap(digit, other.digit);
	else 
		std::swap(digits, other.digits);
}

void container::resize(int size)
{
	if (size < 1)
	{
		delete[] digits;
		throw Size();
	}
	else
	{
		container tmp(*this, size);
		swap(tmp);
	}
}